package at.fhooe.mc.sn;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.net.PortUnreachableException;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;

import at.fhooe.mc.sn.gui.Acc;
import at.fhooe.mc.sn.gui.GUI;
import at.fhooe.mc.sn.gui.SerialMonitor;
import at.fhooe.mc.sn.gui.Temp;

/**
 * Controller for the Smart Sensor Client Application
 * @author Michael
 *
 */
public class SmartSensorJavaClient implements WindowListener, ActionListener, TreeSelectionListener, KeyListener{
	private SmartSensorJavaClient client;
	private SerialCommunication serial;
	private GUI window;
	private SerialMonitor serialMonitor;
	private ProtocolHandler protocol;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		new SmartSensorJavaClient();
	}

	public SmartSensorJavaClient() {
		client = this;
		initGUI();
		connectToArduino();
	}

	public void initGUI() {
		window = new GUI(this);
		window.getFrame().setVisible(true);
		window.getFrame().addWindowListener(client);
	}

	public void connectToArduino() {
		if(serial != null){
			serial.close();
		}
		serial = new SerialCommunication();
		try {
			serial.initialize();

			// serial setup time
			try {
				Thread.sleep(1500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("Serial Connection Started");

			protocol = new ProtocolHandler(serial);
		} catch (PortUnreachableException e1) {
			JOptionPane.showMessageDialog(window.getFrame(), e1.getMessage());
		}
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		serial.close();
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		serial.close();
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() instanceof JMenuItem) {
			if (((JMenuItem)e.getSource()).getText().equalsIgnoreCase("Connect")) {
				connectToArduino();
			} else if(((JMenuItem)e.getSource()).getText().equalsIgnoreCase("Serial Monitor")){
				serialMonitor = new SerialMonitor(protocol);
				serialMonitor.getTextField().addKeyListener(this);
			} else if(((JMenuItem)e.getSource()).getText().equalsIgnoreCase("Reset")){
				if(protocol!=null || protocol.isConnected())
					protocol.request("RESETMINMAX");
				else
					JOptionPane.showMessageDialog(window.getFrame(), "Could not find COM port.");
				
			} else if(((JMenuItem)e.getSource()).getText().equalsIgnoreCase("Exit")){
				serial.close();
				System.exit(0);
			}
		}
	}

	@Override
	public void valueChanged(TreeSelectionEvent e) {
		if(e.getNewLeadSelectionPath().getLastPathComponent().toString().equalsIgnoreCase("Accelerometer")){
			window.getPanel().removeAll();
			window.getPanel().add(new Acc(protocol));
		} else if(e.getNewLeadSelectionPath().getLastPathComponent().toString().equalsIgnoreCase("Temperature")) {
			window.getPanel().removeAll();
			window.getPanel().add(new Temp(protocol));
		}
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		
	}

	@Override
	public void keyReleased(KeyEvent event) {
		if(event.getKeyCode()==KeyEvent.VK_ENTER){
			String command = ((JTextField)event.getSource()).getText();
			String response = protocol.request(command);
			serialMonitor.getTextField_1().setText(response);
		}
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		
	}
}
